﻿using Microsoft.EntityFrameworkCore;
using RecordBill.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RecordBill.Data
{
    public class AppDbContext : DbContext
    {// 定义 DbContext
       
            public DbSet<Transaction> Transactions { get; set; }

            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                // SQLite 数据库文件路径
                optionsBuilder.UseSqlite("Data Source=transactions.db");
            }

            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                // 为微信单号创建唯一索引
                modelBuilder.Entity<Transaction>()
                    .HasIndex(t => t.TransactionId)
                    .IsUnique();

                // 为交易时间创建索引
                modelBuilder.Entity<Transaction>()
                    .HasIndex(t => t.TradeTime);

                // 设置字段最大长度
                modelBuilder.Entity<Transaction>()
                    .Property(t => t.TransactionId)
                    .HasMaxLength(64);
            }
        
    }
}
